<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 原型模式 类似 
        const Person = {
            name: '张三',
            age: 18,

            show() {
                console.log(this.name + '---' + this.age);
            }
        }
        const nPerson = Object.create(Person);
        console.log(nPerson === Person);//false

        //手写深拷贝
        function deepClone(obj) {
            if (typeof obj !== 'object' || obj === null) {
                return obj;
            }
            let newObj = Array.isArray(obj) ? [] : {};
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    newObj[key] = deepClone(obj[key]);
                }
            }
            return newObj;
        }
        //测试
        let obj = { name: '张三', age: 18, show: function () { } };
        let newObj = deepClone(obj);
        console.log(newObj === obj);//false
        console.log(newObj.show === obj.show);//false

        //手写浅拷贝
        //浅拷贝
        function shallowClone(obj) {
            if (typeof obj !== 'object' || obj === null) {
                return obj;
            }
            let newObj = Array.isArray(obj) ? [] : {};
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    newObj[key] = obj[key];
                }
            }
            return newObj;
        }
    </script>
</body>

</html>